package com.cat.dataStructure02;

import java.util.Arrays;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/minimize-maximum-of-array/
 * @create 2025/10/13 20:33
 * @since JDK17
 */

public class Solution15 {
    boolean check(int[] nums, int target) {
        long val = nums[nums.length - 1];
        for (int i = nums.length - 1; i > 0; i--) { // 从后往前推
            val = nums[i - 1] + Math.max(val - target, 0L);
        }

        return val <= target;
    }

    public int minimizeArrayValue(int[] nums) {
        int l = 0, r = Arrays.stream(nums).max().getAsInt(), mid;
        while (l + 1 < r) {
            mid = l + (r - l) / 2;
            if (check(nums, mid)) {
                r = mid;
            } else {
                l = mid;
            }
        }

        return r;
    }
}
